var app = getApp();
import { dealers } from '../../utils/tabBarUrl'
var util = require('../../utils/util.js');
import {date,years,months,days,today} from '../../utils/date';
var one_id = 0;
var two_id = 0;
// 排序
const compare = (key,n) => { //n=-1升序 n=1 降序  n=0不排序
  return (obj1, obj2) => {
    let value1 = obj1[key]*1;
    let value2 = obj2[key]*1; 
    if (value1 < value2) {
      return 1*n;
    } else if (value1 > value2) {
      return -1*n;
    } else {
      return 0
    }    
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sort_n:0,//排序
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    bar_list:['需求池','库存','盘点','明细'],
    status:0,
    types: [],  //一级分类列表
    choose:0,     //一级分类初始索引值
    searcontent: "",   //搜索内容
    page: 1,  
    allpage:1,
    tszhinput:"",     //备注输入框内容
    tszhcontent:"",   //备注内容
    tanimg:"",   //弹窗商品图片
    tanname: "",  //弹窗商品名称
    tanunit: "",  //弹窗商品单位
    tangui:"",   //弹窗规格名称
    tannum:"",   //弹窗商品数量
    hide_good_box: true,  //动画小球的显隐
    stopstart: false,
    sole:0,  //检测上拉是否触底  0未触底 1触底
    total:0,   //购物车角标
    p_total:0,  //盘点车角标
    pand_detail:{},  //缓存盘点详情
    load:'加载中...',
    loading:1,  //正在加载
    shop_ids:[],
    shop_good_ids:[],
    pr_num_str:[],
    checkCount: 0,
    is_checked:{},  //单选
    is_checkall: false,
    isChooseAll: false,   //全选
    // screen_type_list:{'1':'盘盈','2':'盘亏','3':'账实相符'},
    z_screen_type_index:'',
    screen_type_index:'',
    screen_area_list:['过去一天','过去一周','过去一月'],
    z_screen_area_index:-1,
    screen_area_index:-1,
    area_time:0, //时间段开始时间
    is_screen:false,  //筛选下拉
    // 时间周期********
    years,
    year: date.getFullYear(),
    months:months,
    month: 2,
    days:days,
    day: 2,
    value: [date.getFullYear() - 2010, date.getMonth(), date.getDate() - 1],  //默认今天
    time_type: 0,  //时间time切换的初始索引值
    start_time: 0,  //开始时间
    today:today, //今天
    end_time:today,    //结束时间,默认今天
    z_start_time: '', //选择开始时间
    z_end_time:today,   //选择结束时间,默认今天
    c_start_time: 0,  //右窗开始时间
    c_end_time:today, //结束时间,默认今天
    // *****
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pan_top=170/750 * wx.getSystemInfoSync().windowWidth;
    this.setData({
      pan_top:pan_top,
      height: wx.getSystemInfoSync().windowHeight,
      supername: app.globalData.loginInfo.client_abbre,
      unit_height:0.64* wx.getSystemInfoSync().windowHeight,
      specs_h: 143/750*wx.getSystemInfoSync().windowWidth,
      two_specs_h: 330 / 750 * wx.getSystemInfoSync().windowWidth,
      load_num:1
    });
  },
  noneEnoughPeople:function(){
    return;
  },
  // 需求池
  PurchasingList:function(){
    var that = this;
    app.AppHttp("api/Order/getOrderPreview", {
      client_id:app.globalData.loginInfo.id, 
      type:'need' 
    }, function (res) {
      console.log(JSON.stringify(res.data));    
      var orderCount=0;
      var pur_last_id; 
      var pur_last_arr;
      if(res.data.supplier_list.length>0){
        for(var i=0;i<res.data.supplier_list.length;i++){
          orderCount+=res.data.result[res.data.supplier_list[i].id].length; 
          console.log(i+"-----"+orderCount);
        };
        pur_last_arr=res.data.result[res.data.supplier_list[res.data.supplier_list.length-1].id];
        pur_last_id=pur_last_arr[pur_last_arr.length-1].id;  //需求池最后一个商品id(border:none;)
      };
      if(res.data.result[0]){
        orderCount+=res.data.result[0].length;
        if(res.data.supplier_list.length==0){
          pur_last_arr=res.data.result[0];
          pur_last_id=pur_last_arr[pur_last_arr.length-1].id;  //需求池最后一个商品id(border:none;)
        };
      }; 
      console.log(pur_last_id);
      console.log(orderCount);
      that.setData({
        loading:0,
        shop_ids:[],
        shop_good_ids:[],
        pr_num_str:[],
        orderCount:orderCount,
        none_supplier:res.data.none_supplier,  //存在未知供应商
        cate_list:res.data.supplier_list,
        pur_last_id:pur_last_id,
        info:res.data.result,   
        order_ids:res.data.ids,
        checkCount: 0,
        is_checked:{},  //单选
        is_checkall: false,
        isChooseAll: false    //全选
      });
    });
  },
  // 编辑需求池
  edit_xiu:function(e){
    var index=e.currentTarget.dataset.index; //类索引
    var eal=e.currentTarget.dataset.eal;   //商品索引
    var item=this.data.info[index][eal];
    util.showBuyModal(this);
    console.log(index);
    console.log(eal);
    this.setData({
      shop_id:item.id,
      content:'',
      inputNum:'',
      cai_index:index,
      cai_eal:eal,
      isInput:true,
      tanimg: item.pr_cover?item.pr_cover:"",
      tanname: item.pr_name,
      tanunit: item.unit_name,
      tannum: item.nums
    });   
  },
  // 确定修改需求数量
  xiu_confirm:function(){
    if (this.data.inputNum == ''){
      this.setData({
        inputNum:this.data.tannum
      });
    };
    var info = this.data.info;
    var item=this.data.info[this.data.cai_index][this.data.cai_eal];
    if (this.data.inputNum > 9999.99) {
      wx.showToast({
        title: '数量过大',
      })
      return;
    };  
    util.hideBuyModal(this,"isInput");
    var item_id=item.id; //当前商品的id;
    var pr_num_str=this.data.pr_num_str;  //选中商品的nums数组
    if(this.data.is_checked[item_id]){  //当前商品是否为选中的状态
      var pr_num_index=this.data.shop_ids.indexOf(item_id);   //用id查询修改实采值的索引  
      pr_num_str[pr_num_index]=this.data.inputNum * 1;
    };
    info[this.data.cai_index][this.data.cai_eal].nums = this.data.inputNum * 1;
    this.setData({
      pr_num_str:pr_num_str,
      info:info
    });   
  },
  // 单选
  checkboxChange:function(e){
    console.log(e.detail.value);
    var is_checked = {};
    for (var i = 0; i < e.detail.value.length; i++) {
      var value=e.detail.value[i].split(',')[0]
      is_checked[value] = true;
    };
    if(e.detail.value.length==this.data.orderCount*1){
      this.setData({
        checkCount:e.detail.value.length,
        isChooseAll:true,
        is_checkall:true,
        is_checked:is_checked,
      });
    }else{
      this.setData({
        checkCount:e.detail.value.length,
        isChooseAll:false,
        is_checkall:false,
        is_checked:is_checked
      });
    };
  },
  // 全选
  allChoose:function(){
    if (!this.data.isChooseAll == false) {
      this.setData({
        is_checked:{}
      });
    };
    this.setData({
      isChooseAll: !this.data.isChooseAll,
      is_checkall: !this.data.isChooseAll,
      checkCount:!this.data.isChooseAll == true ? this.data.orderCount*1:0
    });
  },
  formSubmit:function(e){
    console.log(e.detail.value.choosegroup);
    console.log(this.data.order_ids);
    var arr=e.detail.value.choosegroup;
    var shop_ids = arr.map(function (item) {
      item=item.split(',')[0];
      return (item);
    });
    var shop_good_ids = arr.map(function (item) {    
      item=item.split(',')[1];
      return (item);
    });
    var pr_num_str = arr.map(function (item) {    
      item=item.split(',')[2];
      return (item);
    });
    console.log(shop_ids);
    console.log(shop_good_ids);
    console.log(pr_num_str);
    this.setData({
      isopera:true,
      shop_ids:shop_ids,
      shop_good_ids:shop_good_ids,
      pr_num_str:pr_num_str
    });
  },
  /**
   * 操作点击
   */
  operate: function () {
    this.setData({
      isopera:false
    })
  },
  // 去需求单页
  toneed_order:function(){
    app.globalData.toneed_order=1;   //导航停在新需求单
    wx.switchTab({
      url:'/pages/dingdan/dingdan'
    });
  },
  // 确认采购
  conf_caigou:function(){
    var that = this; 
    if(that.data.shop_ids.length==0){
      wx.showLoading({
        title:'至少选择一个',
        duration:500
      });
      return;
    };
    that.setData({
      isopera:false
    });
    if(app.globalData.loginInfo.good_dir_set==2 && app.globalData.loginInfo.client_is_store==1 && app.globalData.loginInfo.purchase_type==2){   //模式6直接生成采购单到总部
      console.log(that.data.order_ids);
      wx.navigateTo({
        url: '/pages/dingdan/dingdantianxie/dingdantianxie?shop_ids=' + JSON.stringify(that.data.shop_ids)+'&type='+'caigou'+'&order_ids='+that.data.order_ids+'&is_enquiry='+0+'&pr_num_str='+that.data.pr_num_str
      });
      // that.PurchasingList();
    }else{    //确认到采购池
      let order_id_arr = that.data.order_ids.split(',');
      app.AppHttp('api/Order/needDetailToPurchase', {
        ids:that.data.shop_ids,
        order_ids:order_id_arr,
        pr_num_str:that.data.pr_num_str
      }, function (res) {
        console.log(JSON.stringify(res.data));     
        wx.showToast({
          title:res.data.msg
        });
        that.PurchasingList();
      });
    }
  },
  // 无需采购
  not_caigou:function(){
    var that = this;
    console.log(that.data.order_ids);
    console.log(that.data.shop_good_ids);
    if(that.data.shop_good_ids.length==0){
      wx.showLoading({
        title:'至少选择一个',
        duration:500
      });
      return;
    };
    wx.showModal({
      title: '提示',
      content: '确认无需采购',
      success: function (res) {
        if (res.confirm) {
          app.AppHttp('api/Order/noNeedPurchase', {
            good_ids:that.data.shop_good_ids,
            ids:that.data.order_ids
          }, function (res) {
            console.log(JSON.stringify(res.data));
            wx.showToast({
              title: res.data.msg
            });
            that.setData({
              isopera:false
            });
            that.PurchasingList();
          });
        }
      }
    })   
  },
  // 库存-明细
  upshoplist:function(){
    if(this.data.page==1){
      wx.showLoading({
        title: this.data.load
      });
    }; 
    var that=this;
    app.AppHttp('api/Supplier/inventory_details', {
      openid: app.globalData.openid,
      page: that.data.page,
      limit: 10,
      one_class:one_id,
      keyword:that.data.searcontent
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if(res.data.status==-1){
        that.setData({
          loading:0,
          types: [],
          rightDatas:[],
          allpage:0,
        });   
        wx.hideLoading();
        return;
      }
      that.setData({
        loading:0,
        types: res.data.one_class_list,
        allpage: res.data.page_total
      });
      if(that.data.page==1){
        that.setData({
          sort_n:0,
          rightDatas: res.data.list
        });
      }else{
        that.setData({
          sort_n:0,
          rightDatas: that.data.rightDatas.concat(res.data.list)
        });
      };
      wx.hideLoading();
    });
  },
  // 按库存排序
  sort_kucun:function(){
    var sort_n=this.data.sort_n;
    var list=this.data.rightDatas;
    if(sort_n==0){
      sort_n=-1;
      list.sort(compare(`num`,sort_n));
    }else if(sort_n==-1){
      sort_n=1;
      list.sort(compare(`num`,sort_n));
    }else if(sort_n==1){
      sort_n=0;
      list.sort(compare(`id`,1));
    }
    this.setData({
      rightDatas:list,
      sort_n:sort_n
    });
  },
  // 盘点
  pand_upshoplist:function(){
    if(this.data.page==1){
      wx.showLoading({
        title: this.data.load
      });
    }; 
    var that=this;
    var start_time;
    var select_time=0;
    if(that.data.start_time==0){
      start_time=that.data.area_time;
    }else{
      start_time=Date.parse(new Date(that.data.start_time+'.00:00:00'));
    };
    var end_time = Date.parse(new Date(that.data.end_time+'.23:59:59'));
    if(start_time!=0){
      select_time=start_time+','+end_time
    };
    app.AppHttp('api/Order/inventory_list', {
      openid: app.globalData.openid,
      page:that.data.page,
      limit:10,
      select_time:select_time
    }, function (res) {
      console.log(JSON.stringify(res.data));  
      that.setData({
        loading:0,
        pand_allpage:res.data.page_total
      }); 
      if(that.data.page==1){
        that.setData({  
          pand_arr:res.data.list
        });
      }else{
        that.setData({   
          pand_arr: that.data.pand_arr.concat(res.data.list)
        });
      };
      wx.hideLoading();
    });
  },
  //顶部导航切换
  typeTap: function(event) { 
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    if(event.currentTarget.dataset.index == 0){
      console.log('需求池');
      this.setData({
        status:0
      });
      this.PurchasingList();
    }else if (event.currentTarget.dataset.index == 1) {
      this.setData({
        status:1,  
        page:1,
        load:'加载中...',
        loading:app.globalData.loginInfo.template_id==0?0:1
      }); 
      if(app.globalData.loginInfo.template_id>0){
        var that = this;
        var left_height;
        const query = wx.createSelectorQuery();
          query.select('.gouwu-img-box').boundingClientRect();
          query.exec((res) => {
            console.log(res);
            left_height=res[0].top-120/750*wx.getSystemInfoSync().windowWidth; 
            that.setData({
              left_height:left_height
            });
        });
        this.upshoplist();
        util.car_total(this,-1,2);
      }else{
        this.setData({
          loading:0,
          types:[],
          rightDatas:[]
        });
      }           
    } else if (event.currentTarget.dataset.index == 2) {     
      this.setData({
        status:2,
        page:1,
        load:'加载中...',
        loading:app.globalData.loginInfo.template_id==0?0:1
      });
      if(app.globalData.loginInfo.template_id>0){
        var that = this;
        var left_height;
        const query = wx.createSelectorQuery();
          query.select('.gouwu-img-box').boundingClientRect();
          query.exec((res) => {
            console.log(res);
            left_height=res[0].top-120/750*wx.getSystemInfoSync().windowWidth; 
            that.setData({
              left_height:left_height
            });
        });
        this.upshoplist();
      }else{
        this.setData({
          loading:0,
          types:[],
          rightDatas:[]
        });
      }
    } else if (event.currentTarget.dataset.index ==3) {
      this.setData({
        status:3,
        page:1,
        load:'加载中...',
        loading:1  
      });
      this.pand_upshoplist();
    }
  },
  // 左侧切换
  typeClick:function(e){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    let offsetTop = e.currentTarget.offsetTop;
    this.setData({
      scrollLeft: offsetTop - this.data.left_height/2,
      sole:0,
      choose: e.currentTarget.dataset.id,
      leftChoose:0,
      page: 1,
      load: "加载中..."
    });
    one_id = this.data.types[this.data.choose].one_class;
    two_id = 0;
    this.upshoplist();
  },
  // 新建盘点单
  toNewpand:function(){
    this.setData({
      status:2,
      page:1,
      loading:app.globalData.loginInfo.template_id==0?0:1
    });
    if(app.globalData.loginInfo.template_id>0){
      this.upshoplist();
    }
  },
  searinput: function (e) {
    var that = this;
    if (e.detail.value != that.data.searcontent) {
      that.setData({
        searcontent: e.detail.value,
        page: 1,
        load: '搜索中'
      });
      app.globalData.task.abort();  //中断上一次搜索
      this.upshoplist();
    }
  },
  // 清除搜索
  clear_sear: function () {
    this.setData({
      searcontent: '',
      page: 1
    });
    this.upshoplist();
  },
  // 去购物车页面
  gouWuCheTap: function () {  
    wx.navigateTo({
      url: '/shop_car_space/pages/shop_car/shop_car?supplier_id=' + "-1" + '&supername=' + this.data.supername+ '&is_ku=' + 2
    });
  },
  // 打开特殊需求备注弹窗
  opentszh:function(){
    this.setData({
      tszhinput: this.data.tszhcontent,
      isInput2:true,
      istszh: true
    })
  },
  // 特殊需求备注弹窗输入内容
  tszhinput:function(e){
    this.setData({
      tszhinput: e.detail.value
    });
  },
  // 关闭特殊需求备注弹窗
  tszhcancel:function(){
    this.setData({
      isInput2: false,
      istszh: false
    })
  },
  // 确定并关闭需求备注弹窗
  tszhconfirm:function(){
    this.setData({
      isInput2: false,
      istszh: false,
      tszhcontent: this.data.tszhinput
    })
  },
  // 商品数量弹窗
  itemAddClick: function (event){
    this.numInput(event)
  },
  // 商品数量弹窗
  numInput:function(event){  
    var index = event.currentTarget.id;
    var info = this.data.rightDatas[index];
    if(!info.pand_num && this.data.status==2){
      info.pand_num=0;    
    };
    util.showBuyModal(this);
    this.setData({
      tszhcontent:'',
      content:'',
      isInput: true,
      inputIndex: index,
      inputNum: '',
      tanimg: info.cover?info.cover:'',
      tanname: info.trade_name,
      tanunit: info.unit_name,
      tannum:this.data.status==1?info.pr_num:info.pand_num
    }); 
  },
  confirm:function(event){
    if (this.data.inputNum == ''){
      this.setData({
        inputNum:this.data.tannum
      });
    } 
    var info = this.data.rightDatas[this.data.inputIndex];
    if (this.data.inputNum > 9999.99) {
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    info.pr_num = this.data.inputNum == '' ? 0 : this.data.inputNum * 1;
    util.hideBuyModal(this,"isInput");
    this.setData({
      ["rightDatas[" + this.data.inputIndex + "]"]: info,
      inputNum: ""
    });
    if(info.price_type==1 || info.price_type==2){  //非企业独立版
      app.myMethod.updateGood(-1,0,info.id, info.pr_num, this.data.tszhcontent,info.true_price,info.true_price,2);
    }else if(info.price_type==3){
      if(this.data.good_dir_set==2 && this.data.loginInfo.purchase_type==2){  //模式6
        app.myMethod.updateGood(-1,this.data.group_id,info.id, info.pr_num, this.data.tszhcontent,info.true_price,0,2);
      }else{  //模式1,4及其他
        app.myMethod.updateGood(-1,0,info.id, info.pr_num, this.data.tszhcontent,info.true_price,0,2);   
      }
    }else if(info.price_type==4){
      if(this.data.good_dir_set==2 && this.data.loginInfo.purchase_type==2){  //模式6
        app.myMethod.updateGood(-1,this.data.group_id,info.id, info.pr_num, this.data.tszhcontent,info.true_price,info.true_price,2);
      }else{   //模式1,4及其他
        app.myMethod.updateGood(-1,0,info.id, info.pr_num, this.data.tszhcontent,info.true_price,info.true_price,2);     
      };
    }else if(info.price_type==5){
      if(this.data.good_dir_set==2 && this.data.loginInfo.purchase_type==2){   //模式6
        app.myMethod.updateGood(-1,this.data.group_id,info.id, info.pr_num, this.data.tszhcontent,info.sale_price,info.sale_price,2);
      }else{    //模式1,4及其他
        app.myMethod.updateGood(-1,0,info.id, info.pr_num, this.data.tszhcontent,info.sale_price,info.sale_price,2);
      }
    };
    if (info.pr_num>0){
      util.touchOnGoods(this,event.touches["0"],-1,'nopand',2);
    }else{
      util.car_total(this,-1,2);
    }
    this.setData({
      tszhcontent: '',
      inputIndex:-1
    });    
  },
  // 确定盘点数量
  pan_confirm:function(event){
    if (this.data.inputNum == ''){
      this.setData({
        inputNum:this.data.tannum
      });
    } 
    var info = this.data.rightDatas[this.data.inputIndex];
    if (this.data.inputNum > 9999.99) {
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    info.pand_num = this.data.inputNum == '' ? 0 : this.data.inputNum * 1;
    util.hideBuyModal(this,"isInput");
    this.setData({
      ["rightDatas[" + this.data.inputIndex + "]"]: info,
      inputNum: ""
    });
    var pand_detail=this.data.pand_detail;
    if(info.pand_num>0){
      pand_detail[this.data.inputIndex]=info;
      pand_detail[this.data.inputIndex].detail_remark=this.data.tszhcontent;
      util.touchOnGoods(this,event.touches["0"],-1,'pand',2);
    }else{
      delete pand_detail[this.data.inputIndex];    
    };
    var p_total= Object.keys(pand_detail).length;  //计算对象中有几项
    this.setData({
      p_total:p_total
    });
  },
  // 去盘点单详情
  pandCheTap:function(){
    var pand_detail=this.data.pand_detail;
    var p_total= Object.keys(pand_detail).length;  //计算对象中有几项
    if(p_total>0){
      wx.setStorageSync('pand_detail',pand_detail) //存 
      wx.setStorageSync('p_total',p_total) //存 
      // wx.removeStorageSync('pand_detail') //清除指定的本地数据
      wx.navigateTo({
        url:'pand_detail/pand_detail?ispand='+"no"
      })
    }else{
      wx.showLoading({
        title:'请盘点库存',
        duration:1000
      });
    } 
  },
  // 关闭商品数量弹窗
  cancel: function () {
    util.hideBuyModal(this,"isInput");
    this.setData({
      inputIndex: -1,
      inputNum: "",
      tszhcontent: '',
      tangui: ""
    });
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    this.setValue();  
  },
  setValue: function (event){
    this.setData({
      inputNum: this.data.content
    });
  },
  // 去盘点单详情
  toPDetail:function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'pand_detail/pand_detail?id=' +id +"&ispand="+"yes"
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.caigou_shua=1;
    app.globalData.dingdan_show = 1;   
    this.busPos = {};
    this.busPos['x'] = app.globalData.ww * 0.1;
    this.busPos['y'] = app.globalData.hh * 0.9;
    this.setData({
      group_id:app.globalData.loginInfo.group_id,   //绑定集团id
      pand_detail:wx.getStorageSync('pand_detail')?wx.getStorageSync('pand_detail'):{},
      p_total:wx.getStorageSync('p_total')?wx.getStorageSync('p_total'):0,
    });
    if(app.globalData.loginInfo.client_is_store<0 && app.globalData.type_level==1 || app.globalData.loginInfo.client_is_store==1){
      app.globalData.list =  dealers;
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected:2,
          list: app.globalData.list
        })
      };
    }; 
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      page:1,
      load:'加载中...',
      loading:1
    });
    console.log(this.data.status);
    if(this.data.status==0){
      this.PurchasingList();
    }if(this.data.status==1 || this.data.status==2){   
      if(app.globalData.loginInfo.template_id==0){  //没有继承模板
        this.setData({
          loading:0,
          types:[],
          rightDatas:[]
        });
        return;
      };
      var that = this;
      var left_height;
      const query = wx.createSelectorQuery();
        query.select('.gouwu-img-box').boundingClientRect();
        query.exec((res) => {
          console.log(res);
          left_height=res[0].top-120/750*wx.getSystemInfoSync().windowWidth; 
          that.setData({
            left_height:left_height
          });
      });
      this.upshoplist();
      if(this.data.status==1){
        util.car_total(this,-1,2);
      };  
    }else if(this.data.status==3){
      this.pand_upshoplist();
    }
  },
  // 打开筛选下拉
  open_screen:function(){
    var is_screen=this.data.is_screen;
    if(is_screen){
      util.hideBuyModal(this,"is_screen");
    }else{
      util.showBuyModal(this,-1);
      this.setData({
        is_screen:true
       });
    } 
  },
  // 关闭筛选下拉
  close_screen:function(){
    util.hideBuyModal(this,"is_screen");
  },
  // 选择筛选类型
  chose_screen_type:function(e){
    this.setData({
      z_screen_type_index:e.currentTarget.dataset.index
    });
  },
  // 选择时间段
  chose_area:function(e){
    this.setData({
      z_screen_area_index:e.currentTarget.dataset.index,
      c_start_time: '',
      c_end_time:today,
      z_start_time: '',
      z_end_time:today
    });
  },
  // 打开时间弹窗
  chose_time:function(e){
    util.showBuyModal(this); 
    if(this.data.z_start_time==''){
      this.setData({
        z_start_time:today
      });
    }; 
    this.setData({
      showModalStatus:true
    });
  },
  // 关闭时间弹窗
  hideBuyModal:function(){
    util.hideBuyModal(this,"showModalStatus");
  },
  // 时间周期选中效果
  bcolor: function (e) {
    var val_time=e.currentTarget.dataset.id==1?this.data.z_end_time:this.data.z_start_time;
    if(val_time!=''){
      val_time=(val_time).split('-');
      this.setData({
        value:[val_time[0] - 2010, val_time[1]-1, val_time[2] - 1]
      });
    }
    this.setData({
      time_type: e.currentTarget.dataset.id
    });
  },
  // 年、月、日改变
  bindChange: function (e) {
    util.bindchange_time(e,this,date)
  },
  // 重置时间
  reset_time_but:function(){
    if(this.data.z_start_time!='' || this.data.z_end_time!=today){
      this.setData({ 
        z_start_time: '', //选择开始时间
        z_end_time:today,   //选择结束时间,默认今天
      });
    };    
  },
  // 确认并关闭选择时间周期弹窗
  queding: function () {
    if (Date.parse(new Date(this.data.z_end_time)) < Date.parse(new Date(this.data.z_start_time))){
      wx.showLoading({
        title: '时间不合理',
        duration:1000
      });
    }else{
      util.hideBuyModal(this,"showModalStatus");
      this.setData({
        z_screen_area_index:-1,
        c_start_time: this.data.z_start_time,  //开始时间
        c_end_time: this.data.z_end_time,    //结束时间
      });
    }  
  },
  // 筛选查看
  screen_queding:function(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    var area_time = 0;
    if(this.data.z_screen_area_index==0){  //过去1天
      area_time = Date.parse(new Date(today))-3600000 * 24
    }else if(this.data.z_screen_area_index==1){  //过去1周
      area_time = Date.parse(new Date(today))-3600000 * 24*7
    }else if(this.data.z_screen_area_index==2){  //过去30天
      area_time = Date.parse(new Date(today))-3600000 * 24*30
    };
    this.setData({
      screen_type_index:this.data.z_screen_type_index,
      screen_area_index:this.data.z_screen_area_index,
      area_time:area_time,
      start_time: this.data.c_start_time,
      end_time: this.data.c_end_time,
      loading:1,
      load:'加载中...',
      page:1
    })
    this.pand_upshoplist();
  },
  // 全部重置
  screen_reset_but:function(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      z_screen_type_index:'',
      z_screen_area_index:-1,
      z_start_time: '',
      z_end_time:today,
      c_start_time: '',
      c_end_time:today,
      screen_type_index:'',
      screen_area_index:-1,
      area_time:0,
      start_time:'',
      end_time:today,
      loading:1,
      load:'加载中...',
      page:1
    });
    this.pand_upshoplist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.status==1 || this.data.status==2){
      if (this.data.page < this.data.allpage) {
        this.setData({
          page: this.data.page + 1,
          isChooseAll: false,
          loading:1,
          load: '加载更多'
        });
        this.upshoplist();
      } else {
        wx.showToast({
          title: '没有了'
        });
        this.setData({
          loading:0,
          sole:1
        });
      };
    }else if(this.data.status==3){    //盘点单列表
      if (this.data.page < this.data.pand_allpage) {
        this.setData({
          page: this.data.page + 1,
          isChooseAll: false,
          loading:1,
          load: '加载更多'
        });
        this.pand_upshoplist();
      } else {
        wx.showToast({
          title: '没有了'
        });
        this.setData({
          loading:0,
          sole:1
        });
      };
    };      
  }
})